home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 2.iso / dist / fw_libgtop.idb / usr / freeware / doc / libgtop / timings.c.z / timings.c
C/C++ Source or Header  |  2002-07-08  |  11KB  |  358 lines

  1. /* $Id: timings.c,v 1.4.2.2 1999/09/16 21:08:55 martin Exp $ */
  2.  
  3. /* Copyright (C) 1998-99 Martin Baulig
  4.    This file is part of LibGTop 1.0.
  5.  
  6.    Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
  7.  
  8.    LibGTop is free software; you can redistribute it and/or modify it
  9.    under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 2 of the License,
  11.    or (at your option) any later version.
  12.  
  13.    LibGTop is distributed in the hope that it will be useful, but WITHOUT
  14.    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15.    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16.    for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with LibGTop; see the file COPYING. If not, write to the
  20.    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  21.    Boston, MA 02111-1307, USA.
  22. */
  23.  
  24. #include <locale.h>
  25.  
  26. #include <glibtop.h>
  27. #include <glibtop/open.h>
  28. #include <glibtop/close.h>
  29. #include <glibtop/xmalloc.h>
  30.  
  31. #include <glibtop/parameter.h>
  32.  
  33. #include <glibtop/union.h>
  34. #include <glibtop/sysdeps.h>
  35.  
  36. #include <sys/times.h>
  37. #include <sys/resource.h>
  38.  
  39. #ifndef PROFILE_COUNT
  40. #define PROFILE_COUNT            100000L
  41. #endif
  42.  
  43. #ifndef PROFILE_COUNT_EXPENSIVE
  44. #define PROFILE_COUNT_EXPENSIVE        10000L
  45. #endif
  46.  
  47. #define ELAPSED_UTIME ((unsigned long) elapsed_utime.tv_sec * 1000000 +    (unsigned long) elapsed_utime.tv_usec)
  48. #define ELAPSED_STIME ((unsigned long) elapsed_stime.tv_sec * 1000000 + (unsigned long) elapsed_stime.tv_usec)
  49.  
  50. #define libgtop_timeradd(tvp, uvp, vvp)                        \
  51.     do {                                \
  52.         (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec;        \
  53.         (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec;    \
  54.         if ((vvp)->tv_usec >= 1000000) {            \
  55.             (vvp)->tv_sec++;                \
  56.             (vvp)->tv_usec -= 1000000;            \
  57.         }                            \
  58.     } while (0)
  59. #define libgtop_timersub(tvp, uvp, vvp)                        \
  60.     do {                                \
  61.         (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;        \
  62.         (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;    \
  63.         if ((vvp)->tv_usec < 0) {                \
  64.             (vvp)->tv_sec--;                \
  65.             (vvp)->tv_usec += 1000000;            \
  66.         }                            \
  67.     } while (0)
  68.  
  69. int
  70. main (int argc, char *argv [])
  71. {
  72.     glibtop_union data;
  73.     unsigned c, count, *ptr;
  74.     struct rusage total_start, total_end;
  75.     struct rusage rusage_start, rusage_end;
  76.     struct timeval elapsed_utime, elapsed_stime;
  77.     pid_t pid;
  78.  
  79.     count = PROFILE_COUNT;
  80.  
  81.     setlocale (LC_ALL, "");
  82.     bindtextdomain (PACKAGE, GTOPLOCALEDIR);
  83.     textdomain (PACKAGE);
  84.  
  85.     printf ("%-12s (%-10s): %7s - %9s - %9s\n",
  86.         "Feature", "Flags", "Count", "utime", "stime");
  87.     printf ("-------------------------------------------"
  88.         "---------------\n");
  89.     
  90.     glibtop_init_r (&glibtop_global_server, 0, 0);
  91.  
  92.     getrusage (RUSAGE_SELF, &total_start);
  93.  
  94.     getrusage (RUSAGE_SELF, &rusage_start);
  95.  
  96.     for (c = 0; c < PROFILE_COUNT; c++)
  97.         glibtop_get_cpu (&data.cpu);
  98.  
  99.     getrusage (RUSAGE_SELF, &rusage_end);
  100.  
  101.     libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
  102.               &elapsed_utime);
  103.  
  104.     libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
  105.               &elapsed_stime);
  106.  
  107.     printf ("CPU          (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
  108.         (unsigned long) data.cpu.flags, PROFILE_COUNT,
  109.         (long double) ELAPSED_UTIME / PROFILE_COUNT,
  110.         (long double) ELAPSED_STIME / PROFILE_COUNT);
  111.  
  112.     getrusage (RUSAGE_SELF, &rusage_start);
  113.  
  114.     for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++)
  115.         glibtop_get_mem (&data.mem);
  116.  
  117.     getrusage (RUSAGE_SELF, &rusage_end);
  118.  
  119.     libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
  120.               &elapsed_utime);
  121.  
  122.     libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
  123.               &elapsed_stime);
  124.  
  125.     printf ("Memory       (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
  126.         (unsigned long) data.mem.flags, PROFILE_COUNT_EXPENSIVE,
  127.         (long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE,
  128.         (long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE);
  129.  
  130.     getrusage (RUSAGE_SELF, &rusage_start);
  131.  
  132.     for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++)
  133.         glibtop_get_swap (&data.swap);
  134.  
  135.     getrusage (RUSAGE_SELF, &rusage_end);
  136.  
  137.     libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
  138.               &elapsed_utime);
  139.  
  140.     libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
  141.               &elapsed_stime);
  142.  
  143.     printf ("Swap         (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
  144.         (unsigned long) data.swap.flags, PROFILE_COUNT_EXPENSIVE,
  145.         (long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE,
  146.         (long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE);
  147.  
  148.     getrusage (RUSAGE_SELF, &rusage_start);
  149.  
  150.     for (c = 0; c < PROFILE_COUNT; c++)
  151.         glibtop_get_uptime (&data.uptime);
  152.  
  153.     getrusage (RUSAGE_SELF, &rusage_end);
  154.  
  155.     libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
  156.               &elapsed_utime);
  157.  
  158.     libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
  159.               &elapsed_stime);
  160.  
  161.     printf ("Uptime       (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
  162.         (unsigned long) data.uptime.flags, PROFILE_COUNT,
  163.         (long double) ELAPSED_UTIME / PROFILE_COUNT,
  164.         (long double) ELAPSED_STIME / PROFILE_COUNT);
  165.  
  166.     getrusage (RUSAGE_SELF, &rusage_start);
  167.  
  168.     for (c = 0; c < PROFILE_COUNT; c++)
  169.         glibtop_get_loadavg (&data.loadavg);
  170.  
  171.     getrusage (RUSAGE_SELF, &rusage_end);
  172.  
  173.     libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
  174.               &elapsed_utime);
  175.  
  176.     libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
  177.               &elapsed_stime);
  178.  
  179.     printf ("Loadavg      (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
  180.         (unsigned long) data.loadavg.flags, PROFILE_COUNT,
  181.         (long double) ELAPSED_UTIME / PROFILE_COUNT,
  182.         (long double) ELAPSED_STIME / PROFILE_COUNT);
  183.  
  184.     printf ("\n");
  185.  
  186.     getrusage (RUSAGE_SELF, &rusage_start);
  187.  
  188.     for (c = 0; c < PROFILE_COUNT_EXPENSIVE; c++) {
  189.         ptr = glibtop_get_proclist (&data.proclist, 0, 0);
  190.         glibtop_free (ptr);
  191.     }
  192.  
  193.     getrusage (RUSAGE_SELF, &rusage_end);
  194.  
  195.     libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
  196.               &elapsed_utime);
  197.  
  198.     libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
  199.               &elapsed_stime);
  200.  
  201.     printf ("Proclist     (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
  202.         (unsigned long) data.proclist.flags,
  203.         PROFILE_COUNT_EXPENSIVE,
  204.         (long double) ELAPSED_UTIME / PROFILE_COUNT_EXPENSIVE,
  205.         (long double) ELAPSED_STIME / PROFILE_COUNT_EXPENSIVE);
  206.  
  207.     pid = getpid ();
  208.  
  209.     printf ("\n");
  210.  
  211.     getrusage (RUSAGE_SELF, &rusage_start);
  212.  
  213.     for (c = 0; c < PROFILE_COUNT; c++)
  214.         glibtop_get_proc_state (&data.proc_state, pid);
  215.  
  216.     getrusage (RUSAGE_SELF, &rusage_end);
  217.  
  218.     libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
  219.               &elapsed_utime);
  220.  
  221.     libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
  222.               &elapsed_stime);
  223.  
  224.     printf ("Proc_State   (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
  225.         (unsigned long) data.proc_state.flags, PROFILE_COUNT,
  226.         (long double) ELAPSED_UTIME / PROFILE_COUNT,
  227.         (long double) ELAPSED_STIME / PROFILE_COUNT);
  228.  
  229.     getrusage (RUSAGE_SELF, &rusage_start);
  230.  
  231.     for (c = 0; c < PROFILE_COUNT; c++)
  232.         glibtop_get_proc_uid (&data.proc_uid, pid);
  233.  
  234.     getrusage (RUSAGE_SELF, &rusage_end);
  235.  
  236.     libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
  237.               &elapsed_utime);
  238.  
  239.     libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
  240.               &elapsed_stime);
  241.  
  242.     printf ("Proc_Uid     (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
  243.         (unsigned long) data.proc_uid.flags, PROFILE_COUNT,
  244.         (long double) ELAPSED_UTIME / PROFILE_COUNT,
  245.         (long double) ELAPSED_STIME / PROFILE_COUNT);
  246.  
  247.     getrusage (RUSAGE_SELF, &rusage_start);
  248.  
  249.     for (c = 0; c < PROFILE_COUNT; c++)
  250.         glibtop_get_proc_mem (&data.proc_mem, pid);
  251.  
  252.     getrusage (RUSAGE_SELF, &rusage_end);
  253.  
  254.     libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
  255.               &elapsed_utime);
  256.  
  257.     libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
  258.               &elapsed_stime);
  259.  
  260.     printf ("Proc_Mem     (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
  261.         (unsigned long) data.proc_mem.flags, PROFILE_COUNT,
  262.         (long double) ELAPSED_UTIME / PROFILE_COUNT,
  263.         (long double) ELAPSED_STIME / PROFILE_COUNT);
  264.  
  265.     getrusage (RUSAGE_SELF, &rusage_start);
  266.  
  267.     for (c = 0; c < PROFILE_COUNT; c++)
  268.         glibtop_get_proc_segment (&data.proc_segment, pid);
  269.  
  270.     getrusage (RUSAGE_SELF, &rusage_end);
  271.  
  272.     libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
  273.               &elapsed_utime);
  274.  
  275.     libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
  276.               &elapsed_stime);
  277.  
  278.     printf ("Proc_Segment (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
  279.         (unsigned long) data.proc_segment.flags, PROFILE_COUNT,
  280.         (long double) ELAPSED_UTIME / PROFILE_COUNT,
  281.         (long double) ELAPSED_STIME / PROFILE_COUNT);
  282.  
  283.     getrusage (RUSAGE_SELF, &rusage_start);
  284.  
  285.     for (c = 0; c < PROFILE_COUNT; c++)
  286.         glibtop_get_proc_time (&data.proc_time, pid);
  287.  
  288.     getrusage (RUSAGE_SELF, &rusage_end);
  289.  
  290.     libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
  291.               &elapsed_utime);
  292.  
  293.     libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
  294.               &elapsed_stime);
  295.  
  296.     printf ("Proc_Time    (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
  297.         (unsigned long) data.proc_time.flags, PROFILE_COUNT,
  298.         (long double) ELAPSED_UTIME / PROFILE_COUNT,
  299.         (long double) ELAPSED_STIME / PROFILE_COUNT);
  300.  
  301.     getrusage (RUSAGE_SELF, &rusage_start);
  302.  
  303.     for (c = 0; c < PROFILE_COUNT; c++)
  304.         glibtop_get_proc_signal (&data.proc_signal, pid);
  305.     
  306.     getrusage (RUSAGE_SELF, &rusage_end);
  307.  
  308.     libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
  309.               &elapsed_utime);
  310.  
  311.     libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
  312.               &elapsed_stime);
  313.  
  314.     printf ("Proc_Signal  (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
  315.         (unsigned long) data.proc_signal.flags, PROFILE_COUNT,
  316.         (long double) ELAPSED_UTIME / PROFILE_COUNT,
  317.         (long double) ELAPSED_STIME / PROFILE_COUNT);
  318.  
  319.     getrusage (RUSAGE_SELF, &rusage_start);
  320.  
  321.     for (c = 0; c < PROFILE_COUNT; c++)
  322.         glibtop_get_proc_kernel (&data.proc_kernel, pid);
  323.  
  324.     getrusage (RUSAGE_SELF, &rusage_end);
  325.  
  326.     libgtop_timersub (&rusage_end.ru_utime, &rusage_start.ru_utime,
  327.               &elapsed_utime);
  328.  
  329.     libgtop_timersub (&rusage_end.ru_stime, &rusage_start.ru_stime,
  330.               &elapsed_stime);
  331.  
  332.     printf ("Proc_Kernel  (0x%08lx): %7lu - %9.2Lf - %9.2Lf\n",
  333.         (unsigned long) data.proc_kernel.flags, PROFILE_COUNT,
  334.         (long double) ELAPSED_UTIME / PROFILE_COUNT,
  335.         (long double) ELAPSED_STIME / PROFILE_COUNT);
  336.     
  337.     getrusage (RUSAGE_SELF, &total_end);
  338.  
  339.     libgtop_timersub (&total_end.ru_utime, &total_start.ru_utime,
  340.               &elapsed_utime);
  341.  
  342.     libgtop_timersub (&total_end.ru_stime, &total_start.ru_stime,
  343.               &elapsed_stime);
  344.  
  345.     printf ("-------------------------------------------"
  346.         "---------------\n");
  347.  
  348.     printf ("%-36s %9lu - %9lu\n\n", "TOTAL",
  349.         ELAPSED_UTIME, ELAPSED_STIME);
  350.  
  351.     printf ("All timings are in clock ticks "
  352.         "(1000000 ticks per second).\n\n");
  353.  
  354.     glibtop_close ();
  355.  
  356.     exit (0);
  357. }
  358.